#ifndef __FLASH_H
#define __FLASH_H

#ifdef __cplusplus
extern "C" {
#endif

#include "at32f435_437.h"


#define  CMD_STATUS1         0x05
#define  CMD_WR_ENABLE       0x06
#define  CMD_ERASE_4KBYTE    0x20
#define  CMD_ERASE_32KBYTE   0x52
#define  CMD_READ_DATA       0x03
#define  CMD_PAGE_PROG       0x02
#define  CMD_FAST_READ       0x0B
#define  CMD_DEVICE_ID       0x90

#ifndef PUINT8
typedef unsigned char * PUINT8;
#endif

/* 擦除函数 */
extern void EraseExternal4KFlash_SPI( UINT32 Dst_Addr );                                       //擦除4K
extern void EraseExternal32KFlash_SPI( UINT32 Dst_Addr );                                      //擦除32K
/* 写函数 */
extern void BlukWriteExternalFlash_SPI(UINT32 StarAddr,UINT16 Len,PUINT8 SendBuffer);          //写SPI Flash函数,无限制
extern void PageWriteExternalFlash_SPI(UINT32 StarAddr,UINT16 Len,PUINT8 RcvBuffer);
/* 读函数 */
extern void ReadExternalFlash_SPI( UINT32 StarAddr,UINT16 Len,PUINT8 RcvBuffer );              //读SPI Flash函数,无限制

extern void BlukReadExternalFlash_SPI(UINT32 StarAddr,UINT16 Len,PUINT8 RcvBuffer);            //读取长度必须为16整数倍，好处是读取速度更快
/* 初始化相关 */
extern UINT16 SPIFlash_ReadID(void);
extern void   SPIFlash_Init(void);

uint16_t W25QXX_ReadID(void);       //读取FLASH ID
uint8_t  W25QXX_ReadSR(void);        //读取状态寄存器 
void W25QXX_Write_SR(uint8_t sr);   //写状态寄存器
void W25QXX_Write_Enable(void);  //写使能 
void W25QXX_Write_Disable(void); //写保护
void W25QXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead);   //读取flash
void W25QXX_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);//写入flash
void W25QXX_Erase_Chip(void);       //整片擦除
void W25QXX_Erase_Sector(uint32_t Dst_Addr);//扇区擦除
void W25QXX_Wait_Busy(void);           //等待空闲

#ifdef __cplusplus
}
#endif

#endif
